Skip to content

Create streamed download targets exclusively - #403

Closed
ryanduguid wants to merge 2 commits into
requests:mainfrom
ryanduguid:audit/ponytail-must-fix-20261017
Closed

ryanduguid wants to merge 2 commits into
requests:mainfrom
ryanduguid:audit/ponytail-must-fix-20261017

Conversation

@ryanduguid

@ryanduguid ryanduguid commented Oct 17, 2026 •

Copy link
Copy Markdown

Summary

Open a streamed download destination exclusively, preserving the existing refusal to overwrite when another writer creates the path after validation.

The file is created with os.open(..., O_WRONLY | O_CREAT | O_EXCL) and then wrapped with os.fdopen, so the check and the creation are one step on every Python version the package declares, including 2.7. An existing file still raises StreamingError("File already exists: ..."); other OSErrors propagate unchanged. If wrapping the descriptor fails, it is closed and the original error is re-raised.

Evidence

  • Before: The check-then-open sequence left a race in which opening the destination for writing could truncate the other writer's file.
    After: The download-creation tests pass, covering creation of a new file, a caller-owned stream, preservation of the winning writer's bytes and closing the descriptor when wrapping it fails. The race test fails on main.
  • The full test suite passes on Python 3.13 (Windows and Linux) and on Python 2.7.18, where flake8 requests_toolbelt (the py27-flake8 environment) is also clean. On 2.7, tests/test_x509_adapter.py fails to collect because it imports datetime.timezone; that is unchanged by this PR and happens on main too.

Risk

A concurrent creator now causes the download to fail instead of replacing its file.

Unverified

PyPy, macOS and the hosted CI matrix were not run.

Use os.open with O_CREAT | O_EXCL instead of the 'x' file mode, and map
EEXIST from that call to the existing StreamingError, as Python 2.7 has
neither the 'x' mode nor raise ... from. Wrap the descriptor with
os.fdopen separately and close it if wrapping fails.
@ryanduguid

Copy link
Copy Markdown
Author

Closing this as part of a tidy-up of my open PRs. Happy to reopen or rebase it if it's still useful.

@ryanduguid ryanduguid closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant